/*
 * @author: wizardc
 */

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace Dou.ScreenController
{
    /// <summary>
    /// 水平和垂直布局组件支持横竖屏切换的适配器
    /// </summary>
    [RequireComponent(typeof(HorizontalOrVerticalLayoutGroup))]
    public class HorizontalOrVerticalLayoutGroupAdapter : MonoBehaviour, IUIAdapter
    {
        [HideInInspector]
        public List<HorizontalOrVerticalLayoutGroupData> horizontalOrVerticalLayoutGroupDataList = new List<HorizontalOrVerticalLayoutGroupData>();
        
        private HorizontalOrVerticalLayoutGroup _horizontalOrVerticalLayoutGroup;
        
        void Awake()
        {
            _horizontalOrVerticalLayoutGroup = GetComponent<HorizontalOrVerticalLayoutGroup>();
            ScreenUIAdapterMgr.instance.AddAdapter(this);
        }
        
        public void RefreshAdapter(int uiScreenMode, int uiFineTuningMode)
        {
            HorizontalOrVerticalLayoutAdapterUtil.CopyFormData(_horizontalOrVerticalLayoutGroup, this, uiScreenMode, uiFineTuningMode);
        }
        
#if UNITY_EDITOR

        public void RefreshAdapterInEditor(int uiScreenMode, int uiFineTuningMode)
        {
            HorizontalOrVerticalLayoutAdapterUtil.CopyFormData(GetComponent<HorizontalOrVerticalLayoutGroup>(), this, uiScreenMode, uiFineTuningMode);
        }
        
#endif
        
        void OnDestroy()
        {
            ScreenUIAdapterMgr.instance.RemoveAdapter(this);
        }
    }

    [Serializable]
    public class HorizontalOrVerticalLayoutGroupData
    {
        public bool isInit;

        public RectOffset padding;
        public TextAnchor childAlignment;
        public float spacing;
        public bool childForceExpandWidth;
        public bool childForceExpandHeight;
        public bool childControlWidth;
        public bool childControlHeight;
        public bool childScaleWidth;
        public bool childScaleHeight;
        public bool reverseArrangement;
        
        public List<HorizontalOrVerticalLayoutGroupFineTuningData> fineTuningList = new List<HorizontalOrVerticalLayoutGroupFineTuningData>();
    }
    
    [Serializable]
    public class HorizontalOrVerticalLayoutGroupFineTuningData
    {
        public RectOffset padding;
        public TextAnchor? childAlignment;
        public float? spacing;
        public bool? childForceExpandWidth;
        public bool? childForceExpandHeight;
        public bool? childControlWidth;
        public bool? childControlHeight;
        public bool? childScaleWidth;
        public bool? childScaleHeight;
        public bool? reverseArrangement;
    }
}
